//
//  ControlCategory.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 12/28/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "ControlCategory.h"
#import "Utility.h"

@implementation UILabel(Extension)

+ (UILabel *)newTitleLabel
{
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor clearColor];
    label.font = FONT(FONT_SIZE_TITLE);
    return label;
}

+ (UILabel *)newDescriptionLabel
{
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor clearColor];
    label.font = FONT(FONT_SIZE_DESCRIPTION);
    label.textColor = COLOR_GRAY_DARK;
    return label;
}

+ (UILabel *)newNormalLabel
{
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor clearColor];
    label.font = FONT(FONT_SIZE_NORMAL);
    return label;
}

- (int)fontHeight
{
    return [Utility getLineHeightOfFont:self.font];
}

- (float)widthString
{
    return [Utility getSizeOfString:self.text font:self.font size:CGSizeMake(999, 999) lineBreakMode:self.lineBreakMode].width;
}

@end

@implementation UIButton(Extension)

+ (UIButton*)buttonWithTitle:(NSString*)title target:(id)target selector:(SEL)sel
{
    UIButton *button = [UIButton buttonWithTarget:target selector:sel];
    [button setTitle:title forState:UIControlStateNormal];
    
    float widthString = [button.titleLabel widthString];
    button.frame = RECT(0, 0, widthString, HEI_HEADER_VIEW);
    
    return button;
}

+ (UIButton*)buttonWithImage:(UIImage*)image target:(id)target selector:(SEL)sel
{
    UIButton *button = [UIButton buttonWithTarget:target selector:sel];
    [button setImage:image forState:UIControlStateNormal];
    
    return button;
}

+ (UIButton*)buttonWithTarget:(id)target selector:(SEL)sel
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:COLOR_BLUE_IOS_7 forState:UIControlStateNormal];
    [button setTitleColor:COLOR_BLUE_DARK forState:UIControlStateHighlighted];
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

@end

@implementation UIImage(Extension)

+ (UIImage *)imageNamed:(NSString *)name color:(UIColor *)color
{
    UIImage *image = [UIImage imageNamed:name];
    
    return [image imageTintedWithColor:color];
}

- (UIImage *)imageTintedWithColor:(UIColor *)color
{
	if (color)
    {
		// Construct new image the same size as this one.
		UIImage *image;
		UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".
		CGRect rect = CGRectZero;
		rect.size = [self size];
		
		// tint the image
		[self drawInRect:rect];
		[color set];
		UIRectFillUsingBlendMode(rect, kCGBlendModeDarken);
		
		// restore alpha channel
		[self drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
		
		image = UIGraphicsGetImageFromCurrentImageContext();
		UIGraphicsEndImageContext();
		
		return image;
	}
	
	return self;
}

+ (UIImage *)imageFromColor:(UIColor *)color withRect:(CGRect)imageRect
{
    UIGraphicsBeginImageContext(imageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, imageRect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

+ (UIImage *)imageFromColor:(UIColor *)color
{
    CGRect rect = RECT(0, 0, 1, 1);
    return [UIImage imageFromColor:color withRect:rect];
}
@end

@implementation UIView(Extension)

+ (UIView *)newLineView
{
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = COLOR_GRAY_LIGHT;
    return lineView;
}

@end

@implementation UITextField(Extension)

- (float)floatValue
{
    if (self.text.length == 0)
    {
        return 0;
    }
    
    return [self.text floatValue];
}

@end